﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace GetXsdSQL
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        private static string GetFileName(string path)
        {
            if (path.Contains("\\"))
            {
                string[] arr = path.Split('\\');
                return arr[arr.Length - 1];
            }
            else
            {
                string[] arr = path.Split('/');
                return arr[arr.Length - 1];
            }
        }

        private void OutMsg(string msg)
        {
            rbMessage.AppendText(msg + "\r\n");
            rbMessage.ScrollToCaret();
        }
        public List<FileInfo> GetFilesByDir(string path, string filter = "*.*")
        {
            if (string.IsNullOrEmpty(path) == true) return null;
            DirectoryInfo di = new DirectoryInfo(path);

            //找到该目录下的文件
            FileInfo[] fi = di.GetFiles(filter);

            //把FileInfo[]数组转换为List
            List<FileInfo> list = fi.ToList<FileInfo>();
            return list;
        }
        private void exportSQL(string filename)
        {
            //string sDataSet = System.IO.Path.GetFileName(filename);
            string sDataSet = System.IO.Path.GetFileNameWithoutExtension(filename);
            string sTableName = string.Empty;
            string sRptSQL = string.Empty;
            string sSQL =
                "INSERT INTO 你的报表sql存储表中";

            Hashtable htScript = SchemaXmlHelper.getXSDfileCommand(filename);
            foreach (DictionaryEntry de in htScript)
            {
                sTableName = de.Key.ToString();
                sRptSQL = de.Value.ToString();
                OutMsg("-- " + sDataSet + "." + sTableName + " --");
                if (cbExpotToMySQL.Checked)
                {
                    OutMsg(string.Format(sSQL, sDataSet, sTableName, sRptSQL));
                }
                else
                {
                    OutMsg(sRptSQL);
                }
            }
        }

        private void btnGetSQL_Click(object sender, EventArgs e)
        {
            string filePath = edtPath.Text;
            //System.Diagnostics.Debug.WriteLine("XSD Directory-{0}", filePath);
            if (string.IsNullOrEmpty(filePath)||(!Directory.Exists(filePath))) return;
            if (cbClearResult.Checked) rbMessage.Clear();

            List<FileInfo> list1 = GetFilesByDir(filePath, "*.xsd");
            foreach (FileInfo fi in list1)
            {
                //OutMsg("-----------导出" + fi.FullName.ToString() + "内容-----------");
                exportSQL(fi.FullName.ToString());
            }

        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            if (fbDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                edtPath.Text = fbDialog.SelectedPath;
        }
    }
}
